home *** CD-ROM | disk | FTP | other *** search
/ Collection of Tools & Utilities / Collection of Tools and Utilities.iso / print / gsview10.zip / doc2tex.c < prev    next >
C/C++ Source or Header  |  1993-05-03  |  6KB  |  259 lines

  1. #ifndef lint
  2. static char *RCSid = "$Id: doc2tex.c%v 3.38.2.70 1993/02/08 02:19:29 woo Exp woo $";
  3. #endif
  4.  
  5.  
  6. /*
  7.  * doc2tex.c  -- program to convert Gnuplot .DOC format to LaTeX document
  8.  * Also will work for VMS .HLP files. 
  9.  * Modified by Russell Lang from hlp2ms.c by Thomas Williams 
  10.  * Extended by David Kotz to support quotes ("), backquotes, tables.
  11.  *
  12.  * usage:  doc2tex [file.doc [file.tex]]
  13.  *
  14.  *   where file.doc is a Gnuplot .DOC file, and file.tex will be an
  15.  *     article document suitable for printing with LaTeX.
  16.  *
  17.  * typical usage for GNUPLOT:
  18.  *
  19.  *   doc2tex gnuplot.doc gnuplot.tex 
  20.  *   latex gnuplot.tex ; latex gnuplot.tex
  21.  */
  22.  
  23. #include <stdio.h>
  24. #include <ctype.h>
  25.  
  26. #define MAX_NAME_LEN    256
  27. #define MAX_LINE_LEN    256
  28. #define TRUE 1
  29. #define FALSE 0
  30.  
  31. typedef int boolean;
  32.  
  33. boolean intable = FALSE;
  34. boolean verb = FALSE;
  35.  
  36. main(argc,argv)
  37. int argc;
  38. char **argv;
  39. {
  40. FILE * infile;
  41. FILE * outfile;
  42.     infile = stdin;
  43.     outfile = stdout;
  44.     if (argc > 3) {
  45.         fprintf(stderr,"Usage: %s [infile [outfile]]\n", argv[0]);
  46.         exit(1);
  47.     }
  48.     if (argc >= 2) 
  49.         if ( (infile = fopen(argv[1],"r")) == (FILE *)NULL) {
  50.             fprintf(stderr,"%s: Can't open %s for reading\n",
  51.                 argv[0], argv[1]);
  52.             exit(1);
  53.         }
  54.     if (argc == 3)
  55.         if ( (outfile = fopen(argv[2],"w")) == (FILE *)NULL) {
  56.             fprintf(stderr,"%s: Can't open %s for writing\n",
  57.                 argv[0], argv[2]);
  58.         }
  59.     
  60.     init(outfile);
  61.     convert(infile,outfile);
  62.     finish(outfile);
  63.     exit(0);
  64. }
  65.  
  66.  
  67. init(b)
  68. FILE *b;
  69. {
  70.     (void) fputs("\\input{titlepag.tex}\n",b);
  71. }
  72.  
  73.  
  74. convert(a,b)
  75.     FILE *a,*b;
  76. {
  77.     static char line[MAX_LINE_LEN];
  78.  
  79.     while (fgets(line,MAX_LINE_LEN,a)) {
  80.        process_line(line, b);
  81.     }
  82. }
  83.  
  84. process_line(line, b)
  85.     char *line;
  86.     FILE *b;
  87. {
  88.     switch(line[0]) {        /* control character */
  89.        case '?': {            /* interactive help entry */
  90.           break;            /* ignore */
  91.        }
  92.        case '@': {            /* start/end table */
  93.           if (intable) {
  94.              (void) fputs("\\hline\n\\end{tabular}\n", b);
  95.              (void) fputs("\\end{center}\n",b);
  96.              intable = FALSE;
  97.           } else {
  98.              if (verb) {
  99.                 (void) fputs("\\end{verbatim}\n",b);
  100.                 verb=FALSE;
  101.              } 
  102.              (void) fputs("\n\\begin{center}\n", b);
  103.              (void) fputs("\\begin{tabular}{|ccl|} \\hline\n", b);
  104.              intable = TRUE;
  105.           }
  106.           /* ignore rest of line */
  107.           break;
  108.        }
  109.        case '#': {            /* latex table entry */
  110.           if (intable)
  111.             (void) fputs(line+1, b); /* copy directly */
  112.           else
  113.             fprintf(stderr, "error: # line found outside of table\n");
  114.           break;
  115.        }
  116.        case '%': {            /* troff table entry */
  117.           break;            /* ignore */
  118.        }
  119.        case '\n':            /* empty text line */
  120.        case ' ': {            /* normal text line */
  121.           if (intable)
  122.             break;        /* ignore while in table */
  123.           if (line[1] == ' ') {
  124.              /* verbatim mode */
  125.              if (!verb) {
  126.                 (void) fputs("\\begin{verbatim}\n",b);
  127.                 verb=TRUE;
  128.              }
  129.              (void) fputs(line+1,b); 
  130.           } else {
  131.              if (verb) {
  132.                 (void) fputs("\\end{verbatim}\n",b);
  133.                 verb=FALSE;
  134.              } 
  135.              if (line[0] == '\n')
  136.                puttex(line,b); /* handle totally blank line */
  137.              else
  138.                puttex(line+1,b);
  139.           }
  140.           break;
  141.        }
  142.        default: {
  143.           if (isdigit(line[0])) { /* start of section */
  144.              if (!intable)    /* ignore while in table */
  145.                section(line, b);
  146.           } else
  147.             fprintf(stderr, "unknown control code '%c' in column 1\n", 
  148.                   line[0]);
  149.           break;
  150.        }
  151.     }
  152. }
  153.  
  154. /* process a line with a digit control char */
  155. /* starts a new [sub]section */
  156.  
  157. section(line, b)
  158.     char *line;
  159.     FILE *b;
  160. {
  161.     static char string[MAX_LINE_LEN];
  162.     int sh_i;
  163.  
  164.     if (verb) {
  165.        (void) fputs("\\end{verbatim}\n",b);
  166.        verb=FALSE;
  167.     } 
  168.     (void) sscanf(line,"%d %[^\n]s",&sh_i,string);
  169.     switch(sh_i)
  170.      {
  171.         case 1: 
  172.         (void) fprintf(b,"\\section{");
  173.         break;
  174.         case 2: 
  175.         (void) fprintf(b,"\\section{");
  176.         break;
  177.         case 3:
  178.         (void) fprintf(b,"\\subsection{");
  179.         break;
  180.         case 4: 
  181.         (void) fprintf(b,"\\subsubsection{");
  182.         break;
  183.         default:
  184.         case 5: 
  185.         (void) fprintf(b,"\\paragraph{");
  186.         break;
  187.      }
  188.     if (islower(string[0]))
  189.      string[0] = toupper(string[0]);
  190.     puttex(string,b);
  191.     (void) fprintf(b,"}\n");
  192. }
  193.  
  194. /* put text in string str to file while buffering special TeX characters */
  195. puttex(str,file)
  196. FILE *file;
  197. register char *str;
  198. {
  199. register char ch;
  200. static boolean inquote = FALSE;
  201.  
  202.      while( (ch = *str++) != '\0') {
  203.          switch(ch) {
  204.              case '#':
  205.              case '$':
  206.              case '%':
  207.              case '&':
  208.              case '_':
  209.              case '{':
  210.              case '}':
  211.                  (void) fputc('\\',file);
  212.                  (void) fputc(ch,file);
  213.                  break;
  214.              case '\\':
  215.                  (void) fputs("$\\backslash$",file);
  216.                  break;
  217.              case '~':
  218.                  (void) fputs("\\~{\\ }",file);
  219.                  break;
  220.              case '^':
  221.                  (void) fputs("\\verb+^+",file);
  222.                  break;
  223.              case '>':
  224.              case '<':
  225.              case '|':
  226.                  (void) fputc('$',file);
  227.                  (void) fputc(ch,file);
  228.                  (void) fputc('$',file);
  229.                  break;
  230.              case '"': 
  231.                  /* peek at next character: if space, end of quote */
  232.                  if (*str == '\0' || isspace(*str) || ispunct(*str))
  233.                    (void) fputs("''", file);
  234.                  else
  235.                    (void) fputs("``", file);
  236.                  break;
  237.              case '`':    /* backquotes mean boldface */
  238.                  if (inquote) {
  239.                     fputs("}", file);
  240.                     inquote = FALSE;
  241.                  } else {
  242.                     fputs("{\\bf ", file);
  243.                     inquote = TRUE;
  244.                  }
  245.                  break;
  246.              default:
  247.                  (void) fputc(ch,file);
  248.                  break;
  249.          }
  250.      }
  251. }
  252.  
  253.  
  254. finish(b)
  255. FILE *b;
  256. {
  257.     (void) fputs("\\end{document}\n",b);
  258. }
  259.